package com.example.zjx8969.myapplication.view;


import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.PorterDuff;
import android.graphics.PorterDuffXfermode;
import android.graphics.Rect;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.view.View;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import com.bumptech.glide.Glide;
import com.bumptech.glide.request.target.CustomTarget;
import com.bumptech.glide.request.transition.Transition;
import com.example.zjx8969.myapplication.R;

public class CircleAvatarView extends View {
    private Bitmap innerBitmap, outerBitmap;
    private final Rect rect = new Rect();
    private final Rect rectInside = new Rect();

    private final Paint DEFAULT_PAINT;
    private int insideDiameter;

    public CircleAvatarView(Context context) {
        this(context, null, 0);

    }

    public CircleAvatarView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public CircleAvatarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        DEFAULT_PAINT = new Paint(Paint.DITHER_FLAG | Paint.FILTER_BITMAP_FLAG);
        if (attrs == null) return;
        TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.CircleAvatarView);
        insideDiameter = typedArray.getDimensionPixelSize(R.styleable.CircleAvatarView_inside_diameter, -1);
        int insideSrcId = typedArray.getResourceId(R.styleable.CircleAvatarView_inside_src, -1);
        int outsideSrcId = typedArray.getResourceId(R.styleable.CircleAvatarView_outside_src, -1);
        typedArray.recycle();
        if (insideDiameter == -1 || outsideSrcId == -1)
            throw new IllegalArgumentException(String.format("属性未设置 insideDiameter=%d  outsideSrcId=%d", insideDiameter, outsideSrcId));
        if (insideSrcId != -1)
            innerBitmap = toCircle(BitmapFactory.decodeResource(getResources(), insideSrcId));
        outerBitmap = BitmapFactory.decodeResource(getResources(), outsideSrcId);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        canvas.drawBitmap(outerBitmap, null, rect, DEFAULT_PAINT);
        if (innerBitmap != null) canvas.drawBitmap(innerBitmap, null, rectInside, DEFAULT_PAINT);
    }

    private Bitmap toCircle(@NonNull Bitmap bitmap) {
        int min = Math.min(bitmap.getWidth(), bitmap.getHeight());
        Bitmap dst = Bitmap.createBitmap(min, min, bitmap.getConfig());
        Canvas canvas = new Canvas(dst);
        Paint paint = new Paint();
        paint.setAntiAlias(true);
        int radius = min >> 1;
        canvas.drawCircle(radius, radius, radius, paint);
        paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_IN));
        canvas.drawBitmap(bitmap, 0, 0, paint);
        return dst;
    }

    public void setBitmap(Bitmap bitmap) {
        this.innerBitmap = toCircle(bitmap);
        postInvalidate();
    }

    public void setBitmap(String url) {
        Glide.with(getContext()).asBitmap().load(url).override(rectInside.width()).circleCrop().into(new CustomTarget<Bitmap>() {
            @Override
            public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) {
                innerBitmap = resource;
                postInvalidate();
            }

            @Override
            public void onLoadCleared(@Nullable Drawable placeholder) {

            }
        });
    }

    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);

        rect.left = rect.top = 0;
        rect.right = w;
        rect.bottom = h;

        int padding = (w - insideDiameter) >> 1;
        rectInside.left = rectInside.top = padding;
        rectInside.right = rectInside.bottom = (insideDiameter + padding);
//        Log.d("tag", String.format("onSizeChanged:w=%d h=%d inside=%s,outSide=%s ", w, h, rectInside.toShortString(), rect.toShortString()));
    }
}
